package com.qf.sy2103.ssm.Controller.User;


import com.qf.sy2103.ssm.Service.UserService;
import com.qf.sy2103.ssm.Util.Constant;
import com.qf.sy2103.ssm.entity.User;
import com.qf.sy2103.ssm.vo.Tablevo;
import com.qf.sy2103.ssm.vo.Uservo;
import com.qf.sy2103.ssm.vo.pagevo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    //登录的接口
    @ResponseBody
    @RequestMapping("/login")
    public Uservo login (String real_name, String password, String cap, HttpServletRequest request, HttpSession session) throws IOException {
        log.info("用户名和密码还有验证码分别是 {} {} {}",real_name,password,cap);
        Uservo uservo = new Uservo();
        //获取servlet中生成的验证码
        String code = (String) session.getAttribute("code");//hutool的验证码
        // String captcha = (String) session.getAttribute("captcha");//谷歌的验证码
        if (!(cap.equalsIgnoreCase(code))){
            uservo.setStatus(Constant.USER_LOGIN_FALE);
           uservo.setMessage(Constant.USER_MSG_CODEFILE);
            return uservo;
        }
        User user = userService.findUser(real_name, password);

        if (Objects.isNull(user)){
            uservo.setStatus(Constant.USER_LOGIN_NON);
            uservo.setMessage(Constant.USER_MSG_NOTFIND);
            return uservo;
        }

        //向session中存储登录数据 判断是否可以查询用户
        session.setAttribute("user",user);
        uservo.setStatus(Constant.USER_LOGIN_SUCCESS);
        uservo.setMessage(Constant.USER_MSGL_SUCCESS);
        return uservo;
    }

    //注册的逻辑
    @ResponseBody
    @RequestMapping("/regist")
    public Uservo regist(User user,MultipartFile source, HttpServletRequest request, HttpSession session) throws IOException {
        log.info(user.toString());
        log.info("文件名是 {}",source);
        Uservo uservo = new Uservo();
        if (user.getReal_name().equals("")||user.getPassword().equals("")||user.getEmail().equals("")){
            uservo.setStatus(Constant.USER_REGIST_FALE);
            uservo.setMessage(Constant.USER_MSG_FALE);
            return uservo;
        }
        //上传文件的原始名
        String originalFilename = source.getOriginalFilename();
        //获取上传文件的后缀
        String s = FilenameUtils.getExtension(originalFilename);
        log.info("文件的后缀为 {}",s);
        //生成新的唯一名字
        String newName = UUID.randomUUID().toString();
        //定制全局唯一文件名
        String Name  = newName+"."+s;
        //文件的类型
        String type = source.getContentType();
        log.info("文件名是：{}",Name+"文件类型是：{}",type);
        //获得 upload_file的磁盘路径
        String realPath = session.getServletContext().getRealPath("/upload_file");

        File file = new File(realPath);
        if (file.exists()){
            file.mkdirs();
        }
        //将上传的文件存入磁盘路径中
        source.transferTo(new File(realPath+"\\"+Name));
        log.info(realPath+"\\"+Name);

        //下面的逻辑就是注册
        String name = user.getReal_name();
        User user1 = userService.selectUser(name);
            //如果查询到这个用户名存在 就返回一个用户名存在的页面
            if (Objects.nonNull(user1)){
                uservo.setStatus(Constant.USER_REGIST_FALE);
                uservo.setMessage(Constant.USER_MSG_NAMEIN);
                return uservo;
            }
            user.setFilename(Name);
            int i = userService.addUser(user);
//            session.invalidate();
            uservo.setStatus(Constant.USER_REGIST_SUCCESS);
            uservo.setMessage(Constant.USER_MSG_SUCCESS);
            uservo.setUser(user);
            return uservo;
    }

    //分页展示返回的数据
    @ResponseBody
    @RequestMapping("/getAll")
    public Tablevo<User> getAll(pagevo pagevo){
        log.info("入参是 {}",pagevo);
        Tablevo<User> tablevo = userService.getALLUser(pagevo);
        log.info("返回的数据是 {}",tablevo);
        return tablevo;
    }

    //向数据库中增加数据 del update
    @ResponseBody
    @RequestMapping("/add")
    public Uservo addUser(User user){
        log.info("入参为 {}",user);
        Uservo uservo = new Uservo();
        uservo.setStatus(Constant.USER_VIEWS_SUCCESS);
        uservo.setMessage(Constant.USER_VIEWS_SUCCES);
        if (Objects.isNull(user)){
            //如果修改失败就什么都不给前端的判定if（）空的话就会执行else
            uservo.setMessage(Constant.USER_VIEWS_FULE);
            return uservo;
        }
        user.setFilename("1.txt");
        int i = userService.addUser(user);
        log.info("增加是否成功 {}",i);
        return uservo;
    }

    @ResponseBody
    @RequestMapping("/del")
    //删除数据数据
    public Uservo deleteUser(@RequestParam(value = "ids",required = false) List<Integer> ids){
        log.info("入参为 {}",ids);
        Uservo uservo = new Uservo();
        //调用service层逻辑进行删除
        int i =  userService.deleteUser(ids);
        if (i==0){
            log.info("删除失败");
            uservo.setMessage(Constant.USER_DELETE_FALES);
            return uservo;
//            return "/user/getAll";
        }
        uservo.setStatus(Constant.USER_DELETE_SUCCESS);
        uservo.setMessage(Constant.USER_DELETE_SUCCES);
        log.info("删除成功");
//        return "/user/getAll";
        return uservo;
    }


    @ResponseBody
    @RequestMapping("/update")
    public Uservo updateUser(User user){
        log.info("入参为 {}",user);
        Uservo uservo = new Uservo();
        int i = userService.updateUser(user);
        if (i==0){
            log.info("修改失败");
            uservo.setMessage(Constant.USER_UPDATE_FALES);
            return uservo;
//            return "/user/getAll";
        }
        uservo.setStatus(Constant.USER_UPDATE_SUCCESS);
        uservo.setMessage(Constant.USER_UPDATE_SUCCES);
        log.info("修改成功");
//        return "/user/getAll";
        return uservo;
    }
}
